package edu.nf.work.web.api;

import edu.nf.work.common.ResultVO;
import edu.nf.work.entity.Product;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author XCH
 * @date 2025 - 09 - 17
 */
@RestController
@RequestMapping("/api/v1")
@Tag(name = "商品服务接口", description = "提供商品信息相关的API")
public class ProductController {
    private final static List<Product> products;
    static {
       Product product = new Product(1, "手机", 5000);
       Product product1 = new Product(2, "电脑", 10000);
       Product product2 = new Product(3, "电视", 2000);
       products = Arrays.asList(product, product1, product2);
    }
    @GetMapping("/product")
    @Operation(summary = "查询商品列表", description = "查询所有商品信息", method = "GET")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "500", description = "查询失败，服务器错误")
    })
    public ResultVO<Product> listProducts() {
        return ResultVO.success();
    }
    @GetMapping("/product/{id}")
    @Operation(summary = "查询商品列表", description = "根据id查询商品信息", method = "GET")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "500", description = "查询失败，服务器错误")
    })
    @Parameters({
            @Parameter(name = "id", description = "商品id", required = true,in = ParameterIn.PATH)
    })
    public ResultVO<Product> getProduct(@PathVariable("id") Integer id) {
        for (Product product : products) {
            if (product.getId().equals(id)) {
                return ResultVO.success(product);
            }
        }
        return ResultVO.error(404, "商品不存在");
    }
    @PostMapping("/product")
    @Operation(summary = "添加商品", description = "添加商品信息", method = "POST")
    @Parameters({
            @Parameter(name = "product", required = true)
    })
    public ResultVO<Product> addProduct(@RequestBody Product product) {
        return ResultVO.success();
    }

    @PutMapping("/product")
    @Operation(summary = "修改商品", description = "修改商品信息", method = "PUT")
    @Parameters({
            @Parameter(name = "product", required = true)
    })
    public ResultVO<Product> updateProduct(@RequestBody Product product) {
        return ResultVO.success();
    }
    @DeleteMapping("/product/{id}")
    @Operation(summary = "删除商品", description = "根据id删除商品信息", method = "DELETE")
    @Parameters({
            @Parameter(name = "id", description = "商品id", required = true,in = ParameterIn.PATH)
    })
    public ResultVO<Product> deleteProduct(@PathVariable("id") Integer id) {
        return ResultVO.success();
    }

}
